package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.CompanyMoveInfEntity;
import com.cheng.data.mapper.CompanyMoveInfMapper;
import com.cheng.data.vo.CompanyMoveInfVO;
import com.cheng.data.vo.NoticeVO;
import com.cheng.data.vo.StatisticsVO;
import com.cheng.service.CompanyMoveInfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/12/18 10:38
 * @version: 1.0
 * @description: 企业迁移处理
 */
@Service
public class CompanyMoveInfServiceImpl implements CompanyMoveInfService {

    private final static Logger log = LoggerFactory.getLogger(CompanyMoveInfServiceImpl.class);

    @Autowired
    private CompanyMoveInfMapper companyMoveInfMapper;

    /**
    * @author: ChengJW
    * @params: [companyMoveInfVO]
    * @return: void
    * @date: 2021/12/18
    * @description: 新增企业信息迁出
    */
    @Override
    public void insertCompanyMoveInf(CompanyMoveInfEntity companyMoveInfEntity) throws CommonException {
        companyMoveInfMapper.insert(companyMoveInfEntity);
    }

    /**
     * @author: ChengJW
     * @params: [companyMoveInfEntity]
     * @return: void
     * @date: 2021/12/18
     * @description: 查询代迁入企业列表
     */
    @Override
    public List<CompanyMoveInfVO> queryCompanyMoveList(Map<String,Object> map) throws CommonException {
        return companyMoveInfMapper.queryCompanyMoveList(map);
    }

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: void
    * @date: 2021/12/18
    * @description: 修改企业迁移信息表
    */
    @Override
    public void updateCompanyMoveInf(CompanyMoveInfVO vo) throws CommonException {

        QueryWrapper<CompanyMoveInfEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(vo.getCmCreditCode()),"cm_Credit_Code",vo.getCmCreditCode());
        wrapper.eq(StringUtil.isNotNull(vo.getCmMvInCode()),"cm_Mv_In_Code",vo.getCmMvInCode());
        List<CompanyMoveInfEntity> list = companyMoveInfMapper.selectList(wrapper);
        if (list != null && list.size() > 0){
            for (CompanyMoveInfEntity entity : list){
                //修改企业迁移完成标志为 0 表示已经迁移完成
                if (StringUtil.isNotNull(vo.getCmMvCd())) {
                    entity.setCmMvCd(CodeUtil.MOVE_STD_0);
                    entity.setUtDt(new Date());
                    entity.setCmMvInDt(new Date());
                    companyMoveInfMapper.updateById(entity);
                }
            }
        }
    }

    /**
    * @author: ChengJW
    * @params: [map]
    * @return: java.util.List<com.cheng.data.vo.StatisticsVO>
    * @date: 2021/12/20
    * @description: 统计迁移情况
    */
    @Override
    public List<StatisticsVO> queryStatisticsMvVOList(Map<String, Object> map) throws CommonException {
        if (map == null){
            return null;
        }
        if (CodeUtil.STATISTIC_TYPE.equals(map.get(CodeUtil.STATISTIC_CODE))){
            return companyMoveInfMapper.queryStatisticsMvVOList(map);
        }
        return companyMoveInfMapper.queryStatisticsMvVOListByCode(map);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: com.cheng.data.vo.NoticeVO
     * @date: 2021/12/21
     * @description: home 页面待迁入企业提醒
     */
    @Override
    public NoticeVO queryMoveNoticeVO(Map<String, Object> map) throws CommonException {
        return companyMoveInfMapper.queryMoveNoticeVO(map);
    }
}
